﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using OrganizedIN.BAL.Common;

namespace OrganizedIN.UI.Videos
{
    public partial class add_to_videoalbum : System.Web.UI.Page
    {
        string strUserName;
        DataSet dsalbum = new DataSet();
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                string id = Request.QueryString["id"];
                strUserName = Convert.ToString(Session["UserID"]);

                if (!IsPostBack)
                {
                    dsalbum = Common.getVideoAlbum(strUserName);
                    if (dsalbum.Tables[0].Rows.Count > 0)
                    {
                        DataSet dsvideo = new DataSet();
                        dsvideo = Common.getVideoDetail(strUserName);
                        if (dsvideo.Tables[0].Rows.Count > 0)
                        {
                            dgVideoAlbum.DataSource = dsvideo.Tables[0];
                            dgVideoAlbum.DataBind();

                            if (dsalbum.Tables[0].Rows.Count > 0)
                            {
                                string albumid = "";
                                DropDownList ddl = null;

                                for (int j = 0; j < dsvideo.Tables[0].Rows.Count; j++)
                                {
                                    for (int i = 0; i < dsalbum.Tables[0].Rows.Count; i++)
                                    {
                                        albumid = dsalbum.Tables[0].Rows[i][0].ToString();
                                        ListItem li = new ListItem(dsalbum.Tables[0].Rows[i][1].ToString(), dsalbum.Tables[0].Rows[i][0].ToString());
                                        ddl = (DropDownList)dgVideoAlbum.Items[j].Cells[2].FindControl("drpAlbum");
                                        ddl.Items.Add(li);
                                        if (albumid == dsvideo.Tables[0].Rows[j][5].ToString())
                                        {
                                            ddl.Items.FindByValue(albumid).Selected = true;
                                        }
                                    }

                                }
                            }
                            /*DropDownList ddl = (DropDownList)dgRating.Items[0].Cells[2].FindControl("ddlRating");
                            ddl.Items.FindByValue(rate).Selected = true;*/

                        }
                        else
                        {
                            gridwrapper.InnerHtml = "<h2>Sorry! No records found</h2>";
                        }
                    }
                    else
                    {
                        gridwrapper.InnerHtml = "<h2>Sorry! No records found</h2>";
                    }
                }

            }
            catch (Exception ex)
            {
                gridwrapper.InnerHtml = "<h2>Sorry! Some error occured</h2>" + ex.ToString();
            }
        }

        protected void dgVideoAlbum_EditCommand(object source, DataGridCommandEventArgs e)
        {
            try
            {
                int id = Convert.ToInt32(e.Item.Cells[0].Text);
                DropDownList ddlalbum = (DropDownList)e.Item.Cells[2].FindControl("drpAlbum");
                string albumid = ddlalbum.SelectedValue;
                DataSet ds = new DataSet();
                ds = Common.updateVideoAlbum(strUserName, id, albumid);
                Response.Redirect("myvideos.aspx");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
